/**
 * 管理界面
 */
Ext.define('cc.self.view.MngSelfBus', {
	extend: 'Ext.container.Container',
	uses: ['cc.self.util.SelfService',
	       'cc.self.view.SelfBusGridView',
	       'cc.self.view.SelfBus',
	       'bmc.common.comp.EntityWindow'],
	layout: {
		type: 'hbox',
		align: 'stretch'
	},
	
	viewModel: {
		data: {
			auth: {
				create: true,
				update: true,
				'delete': true
			},
			selfBus: null
		}
	},
	
	initComponent: function(){
		var me = this;
		
		me.selfBusGridView = Ext.create('cc.self.view.SelfBusGridView', {
			flex: 1,
			listeners: {
				//切换viewModel
				selectionchange: function(scope, recs){
					me.getViewModel().set('selfBus', recs[0]);
				}
			},
			tbar: [{
				text: '新增',
				iconCls: 'c-btn-create',
				bind: {
					disabled: '{!auth.create}'
				},
				handler: function(){
					var rec = Ext.create('cc.self.model.SelfBus', {
						id: 0
					});
					CC.Self.createSelfBus({data: '{"id": 0}'}, function(data){
						rec.set('id', data.id);
						rec.commit();
						me.selfBusGridView.getStore().insert(0, rec);
						me.showSelfBusWin('新增', rec, true);
					});
					
				}
			}, {
				text: '编辑',
				iconCls: 'c-btn-update',
				bind: {
					disabled: '{!auth.update}'
				},
				handler: function(){
					var r = me.getViewModel().get('selfBus');
					if(r){
						me.showSelfBusWin('编辑', r, false);
					}
				}
			}, {
				text: '删除',
				iconCls: 'c-btn-delete',
				bind: {
					disabled: '{!auth.delete}'
				},
				handler: function(){
					var r = me.getViewModel().get('selfBus');
					if(r){
						Ext.Msg.confirm('提示', '即将删除该记录，是否继续？', function(btn){
							if(btn == 'yes'){
								CC.Self.deleteSelfBus(r.get('id'), function(){
									me.selfBusGridView.store.remove(r);
								});
							}
						});
					}
				}
			},'-',{
				text: '刷新',
				iconCls: 'c-btn-refresh',
				handler: function(){
					me.cleanSearchFlds();
					delete me.selfBusGridView.store.proxy.extraParams.ywy;
					delete me.selfBusGridView.store.proxy.extraParams.code;
					delete me.selfBusGridView.store.proxy.extraParams.bdt;
					delete me.selfBusGridView.store.proxy.extraParams.edt;
					me.selfBusGridView.store.load();
				}
			},'-',{
				text: '最近一周记录',
				iconCls: '',
				handler: function() {
					me.searchRecords('week');
				}
			},{
				text: '最近一月记录',
				iconCls: '',
				handler: function() {
					me.searchRecords('month');
				}
			},'-',{
				xtype: 'textfield',
				labelAlign: 'right',
				emptyText: '业务员',
				bind: '{ywy}'
			},{
				xtype: 'textfield',
				labelAlign: 'right',
				emptyText: '编码',
				bind: '{code}'
			},{
				xtype: 'datefield',
				labelAlign: 'right',
				emptyText: '报关起始时间',
				format: 'Y-m-d',
				bind: '{bdt}'
			},{
				xtype: 'datefield',
				labelAlign: 'right',
				emptyText: '报关结束时间',
				format: 'Y-m-d',
				bind: '{edt}'
			},{
				xtype: 'button',
				text: '查询',
				iconCls: 'cc_data_search',
				handler: function() {
					me.searchRecords();
				}
			}]
		});
		me.items = [me.selfBusGridView];
		
		me.callParent(arguments);
	},
	
	//显示实体窗口
	showSelfBusWin: function(title, rec, add){
		var me = this;
		var form = Ext.create('cc.self.view.SelfBus', {
			viewModel: {
				data: {
					rec: rec
				}
			}
		});
		
		var win = Ext.create('bmc.common.comp.EntityWindow', {
			width: 915,
			height: 600,
			title: title,
			form: form,
			cb: function(){
				if(add){
					me.selfBusGridView.store.insert(0, rec);
				}
			},
			listeners: {
				reject: function(){
					rec.reject();
				}
			}
		});
		win.show();
	},
	/**
	 * 清除查询域
	 */
	cleanSearchFlds: function() {
		var me = this;
		me.getViewModel().set('bdt', null);
		me.getViewModel().set('edt', null);
		me.getViewModel().set('code', null);
		me.getViewModel().set('ywy', null);
	},
	searchRecords: function(timeMode) {
		var me = this;
		var time = new Date();
		if(timeMode == 'week') {
			var day = time.getDay() + 6;
			time = Ext.Date.subtract(time, Ext.Date.DAY, day); //上周一
			me.getViewModel().set('bdt', new Date(time.getFullYear(), time.getMonth(), time.getDate(), 0, 0, 0));
			time = Ext.Date.add(time, Ext.Date.DAY, 6); //周日
			me.getViewModel().set('edt', new Date(time.getFullYear(), time.getMonth(), time.getDate(), 23, 59, 59));
		}else if(timeMode == 'month') {
			time.setDate(1);
			time = Ext.Date.subtract(time, Ext.Date.MONTH, 1);
			me.getViewModel().set('bdt', new Date(time.getFullYear(), time.getMonth(), time.getDate(), 0, 0, 0));
			time = Ext.Date.getLastDateOfMonth(time);
			me.getViewModel().set('edt', new Date(time.getFullYear(), time.getMonth(), time.getDate(), 23, 59, 59));
		}
		var bdt = Ext.Date.format(me.getViewModel().get('bdt'),'Y-m-d H:i:s');
		var edt = me.getViewModel().get('edt');
		var code = me.getViewModel().get('code');
		var ywy = me.getViewModel().get('ywy');
		var store = me.selfBusGridView.store;
		delete store.proxy.extraParams.ono;
		delete store.proxy.extraParams.bdt;
		delete store.proxy.extraParams.edt;
		delete store.proxy.extraParams.ywy;
		delete store.proxy.extraParams.code;
		if(bdt) {
			Ext.apply(store.proxy.extraParams, {bdt: bdt});
		}
		if(edt) {
			edt = Ext.Date.format(new Date(edt.getFullYear(), edt.getMonth(), edt.getDate(), 23, 59, 59), 'Y-m-d H:i:s');
			Ext.apply(store.proxy.extraParams, {edt: edt});
		}
		if(code) {
			Ext.apply(store.proxy.extraParams, {code: code});
		}
		if(ywy) {
			Ext.apply(store.proxy.extraParams, {ywy: ywy});
		}
		store.loadPage(1);
	}
});
